//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    infoMess:'',
    companyName:'',
    meterAddress:'',
    datas:{"aa":11,"bb":22},
    items:[],
    pictures: [
      'https://p0.meituan.net/movie/ea4ac75173a8273f3956e514a4c78018253143.jpeg',
      'https://p0.meituan.net/movie/5d4fa35c6d1215b5689257307c461dd2541448.jpeg',
      'https://p0.meituan.net/movie/0c49f98a93881b65b58c349eed219dba290900.jpg',
      'https://p1.meituan.net/movie/45f98822bd15082ae3932b6108b17a01265779.jpg',
      'https://p1.meituan.net/movie/722de9a7b0c1f9c262162d87eccaec7c451290.jpg',
      'https://p0.meituan.net/movie/cb9be5bbedb78ce2ef8e83c93f83caca474393.jpg',
      'https://p1.meituan.net/movie/a852b992cdec15319c717ba9fa9b7a35406466.jpg',
      'https://p1.meituan.net/movie/dc1f94811793e9c653170cba7b05bf3e484939.jpg'
    ]
    // motto: 'Hello World',
    // userInfo: {},
    // hasUserInfo: false,
    // canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  previewImage: function (e) {
    var that = this,
      index = e.currentTarget.dataset.index,
      pictures = this.data.pictures;
    wx.previewImage({
      current: pictures[index],
      urls: pictures
    })
  },
  companyNameInput: function (e) {
    this.setData({
      companyName: e.detail.value
    })
  },
  meterAddressInput: function (e) {
    this.setData({
      meterAddress: e.detail.value
    })
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  searchBtnClick: function () {
    if (this.data.companyName.length == 0 && this.data.meterAddress.length == 0) {
      this.setData({
        infoMess: '温馨提示：请输入单位名称或表计地址！',
      })
    } else {
      this.setData({
        infoMess: '',
        items: [
          {
            "url": "http://127.0.0.1/1.flv",
            "title": "201707000000883",
            "netWater":2,
            "sumWater": 332532
          },
          {
            "url": "http://127.0.0.1/2.flv",
            "title": "201707000000884",
            "netWater": 0,
            "sumWater":10232
          }
        ]
      })
      wx.request({
        url: 'https://www.baidu.com',
        method: 'GET',
        data: {
          "aa":1
          // companyName: this.data.companyName,
          // meterAddress: this.data.meterAddress
        },
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          console.log(res.data)
        }
      })
    }
  },
  //重置按钮点击事件
  resetBtnClick: function (e) {
    wx.showToast({
      title: this.data.datas.aa +"",
      icon: 'loading',
      duration: 10000
    })

    setTimeout(function () {
      wx.hideToast()
    }, 1000)

    this.setData({
      infoMess: '',
      companyName: '',
      meterAddress:''
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
